<?php

class SearchController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

	public function tagAction()
	{
		$pageNum = $this->getParam ( "page" );
		$tag = $this->getParam("tag");
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$where = $phoneDb->getAdapter()->quoteInto("t.tag_name = ?", $tag);
		
		$query = $phoneDb->select();
		$query->setIntegrityCheck(false);
		$query->from(array('p' => 'phone'));
		$query->join(array('t' => 'tag'), 'p.id = t.phone_id');
		$query->where($where);
		$phones = $phoneDb->fetchAll ( $query );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 9 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		
		$this->view->paginator = $paginator;
		$this->view->tag = $tag;
		
		$this->_helper->viewRenderer("index");
	}
	
	public function searchAction()
	{
		$pageNum = $this->getParam ( "page" );
		$input = $this->_request->getParam("search-text", "");
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$whereTag = $phoneDb->getAdapter()->quoteInto("t.tag_name LIKE ?", '%'.$input.'%');
		$wherePhone = $phoneDb->getAdapter()->quoteInto("p.name LIKE ?", '%'.$input.'%');
		$whereCatagory = $phoneDb->getAdapter()->quoteInto("c.name LIKE ?", '%'.$input.'%');
		
		$query = $phoneDb->select();
		$query->setIntegrityCheck(false);
		$query->from(array('p' => 'phone'));
		$query->joinLeft(array('t' => 'tag'), 'p.id = t.phone_id', array());
		$query->joinLeft(array('c' => 'catagory'), 'p.catagory_id = c.id', array());
		$query->where($whereTag);
		$query->orWhere($wherePhone);
		$query->orWhere($whereCatagory);
		$query->group("p.id");
		
		$phones = $phoneDb->fetchAll ( $query );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage ( 9 );
		
		if ($pageNum != null) {
			$paginator->setCurrentPageNumber ( $pageNum );
		} else {
			$paginator->setCurrentPageNumber ( 1 );
		}
		
		
		$this->view->paginator = $paginator;
		$this->_helper->viewRenderer("index");
	}
	
	public function advancesearchAction() 
	{
		$name = $this->_request->getParam("search-name", "");
		$minPrice = (int) $this->_request->getParam("min-price", 0);
		$maxPrice = (int) $this->_request->getParam("max-price", 99999999);
		
		// get all phones's data
		$phoneDb = new Application_Model_DbTable_Phone ();
		$whereName = $phoneDb->getAdapter()->quoteInto("p.name LIKE ?", '%'.$name.'%');
		$whereMin = $phoneDb->getAdapter()->quoteInto("p.price >= ?", $minPrice);
		$whereMax = $phoneDb->getAdapter()->quoteInto("p.price <= ?", $maxPrice);
		
		$query = $phoneDb->select();
		$query->setIntegrityCheck(false);
		$query->from(array('p' => 'phone'));
		if ( $name ) {
			$query->where($whereName);
		}
		if ( $minPrice ) {
			$query->where($whereMin);
		}
		if ( $maxPrice ) {
			$query->where($whereMax);
		}
		
		$phones = $phoneDb->fetchAll ( $query );
		
		$paginator = Zend_Paginator::factory ( $phones );
		$paginator->setDefaultItemCountPerPage (9999);
		
		$this->view->paginator = $paginator;
		$this->_helper->viewRenderer("index");
		$this->query = $query->__toString();
		//echo $query->__toString();
	}
	
}

